package com.gwq.computed;

import com.gwq.entity.Equation;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Computed {


    private static List<Equation> equationList = new ArrayList<>();


    public static void main(String[] args) {


        printHeader();
        generateEquations();
        printExercise();
        printCalculations();

    }

    public static  void generateEquations(){

        for(int i=0;i<50;i++){
            Equation equation = generateEquation();

            do{
                equation = generateEquation();
            } while(equationList.contains(equation));
            equationList.add(equation);
        }

    }

    //加法
    private static Equation generateAdditionEquation(){

        Integer n1;
        Integer n2;
        do{
            n1 = generateOperand();
            n2 = generateOperand();

        }while (!check(n1,0,100)||!check(n2,0,100)||!check(n1+n2,0,100));


        Equation equation = new Equation();
        equation.setOperator("+");
        equation.setLeftOperand(n1);
        equation.setRightOperand(n2);
        equation.setSum( (Integer) n1+(Integer) n2);

        return equation;
    }

    //减法
    private static Equation generateSubstractEquation(){
        Integer n1;
        Integer n2;
        do{
            n1 = generateOperand();
            n2 = generateOperand();

        }while (!check(n1,0,100)||!check(n2,0,100)||!check(n1-n2,0,100));

        Equation equation = new Equation();
        equation.setOperator("-");
        equation.setLeftOperand(n1);
        equation.setRightOperand(n2);
        equation.setSum((Integer) n1-(Integer) n2);
        return equation;
    }

    //生成算式
    private static Equation generateEquation(){

        Random random = new Random();
        Integer ov =  random.nextInt(2);

        if(ov==1){
            return  generateAdditionEquation(); //加法
        }else {
            return  generateSubstractEquation(); //减法
        }
    }


    //生成数
    private static Integer generateOperand(){
        Random random = new Random();
        Integer num = random.nextInt(101);
        return num;
    }

    //校验
    private  static boolean check(Integer anInteger,Integer low,Integer high){
        return anInteger>low && anInteger<high;
    }



    public static void printHeader(){
        System.out.println("--------------------------------------------------------");
        System.out.println("-  程序输出50道100以内的加减法算式的习题  -");
        System.out.println("-  每次运行程序可得到一套50道题的习题答案  -");
        System.out.println("--------------------------------------------------------");
    }


    public static void printExercise(){
        int count = 0;
        for(int i=0;i<50;i++){
            String p = equationList.get(i).getLeftOperand()+equationList.get(i).getOperator()+equationList.get(i).getRightOperand()+"=";
            System.out.print(i+1+":   "+p+ "               " + "\t");
            count++;
            if (count % 5 == 0) {
                System.out.println();
            }
        }
    }

    public static void printCalculations(){
        System.out.println("--------------------------------------------------------");
        System.out.println("-  下面是习题的参考答案  -");
        System.out.println("--------------------------------------------------------");
        int count = 0;
        for(int i=0;i<equationList.size();i++){
            System.out.print(i+1+":   "+equationList.get(i).getSum()+ "          " + "\t");
            count++;
            if (count % 5 == 0) {
                System.out.println();
            }
        }
    }


}
